<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="合作机构" prop="memberOrganizeId">
        <MemberOrganizationSelect v-model="queryParams.memberOrganizeId" />
      </el-form-item> -->
      <el-form-item label="合作机构" prop="memberOrganizeId">
        <MemberOrganizationSelectTop v-model="queryParams.memberOrganizeId" />
      </el-form-item>
      <el-form-item label="作业日期" prop="waveDate">
        <el-date-picker
          v-model="queryParams.waveDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="handleCreateOrder()"
          v-hasPermi="['fulfill:orderitem:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 生成送货单
        </el-button> -->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['fulfill:wave:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column v-for="item in header" :key="item" :prop="item" :label="item" show-overflow-tooltip min-width="200">
		     <template #default="scope">
			      <span v-if="scope.column.property === 'flag'">
				      <el-tag type="success" v-for="every in scope.row[scope.column.property]" :key="every" size="mini" style="margin: 0 2px;">{{ every }}</el-tag>
			      </span>
			      <span v-else>{{ scope.row[scope.column.property] }}</span>
		     </template>
	    </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { WaveApi } from '@/api/fulfill/wave'
import MemberOrganizationSelectTop from '@/views/member/organization/components/MemberOrganizationSelectTop.vue'

/** 履约单明细 列表 */
// defineOptions({ name: 'OrderItem' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const header = ref([]) // 列表的数据


const queryParams = reactive({
  waveDate: [],
  // orderId: undefined,
  memberOrganizeId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WaveApi.generateWave(queryParams)
    if (data !== null) {
      list.value = data.tableDetail
      header.value = data.tableHeader;
    } else { 
      list.value = []
      header.value=[]
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}



/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WaveApi.exportWave(queryParams)
    download.excel(data, queryParams.memberOrganizeId + '波次单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  //getList()
  loading.value = false
})
</script>